{
 "cells": [
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "markdown",
   "source": [
    "# 一、ndarray\n",
    "## 1.ndarry的特性\n",
    "### 多维性"
   ],
   "id": "9eae49930200172a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T01:44:09.974604Z",
     "start_time": "2025-10-13T01:44:09.853927Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "628b6ff72bc2fad7",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:11:31.721370Z",
     "start_time": "2025-10-09T07:11:31.716866Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array(5) # 创建0维的ndarray\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim) # 数组的维度：number of dimensions"
   ],
   "id": "ce7b9e60164a005d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "arr的维度： 0\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:13:23.442110Z",
     "start_time": "2025-10-09T07:13:23.437613Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2,3]) # 创建1维的ndarray\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim) # 数组的维度：number of dimensions"
   ],
   "id": "dde0bea4216ff42a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "arr的维度： 1\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:15:07.802375Z",
     "start_time": "2025-10-09T07:15:07.797946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]]) # 创建2维的ndarray\n",
    "print(arr)\n",
    "print('arr的维度：',arr.ndim) # 数组的维度：number of dimensions"
   ],
   "id": "44a73f0afd297b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "arr的维度： 2\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 同质性",
   "id": "adee45dbcd551687"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:21:10.667826Z",
     "start_time": "2025-10-09T07:21:10.663470Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,'hello']) # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "211cb1899e8e8e59",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1' 'hello']\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:21:41.121968Z",
     "start_time": "2025-10-09T07:21:41.117938Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2.5]) # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "e123ec12c1eb1d9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  2.5]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.ndarray的属性",
   "id": "e0cd52a5024a4bb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:42:08.883623Z",
     "start_time": "2025-10-09T07:42:08.878636Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array(1)\n",
    "print(arr)\n",
    "print('数组的形状：',arr.shape)\n",
    "print('数组的维度：',arr.ndim)\n",
    "print('元素的个数：',arr.size)\n",
    "print('元素的数据类型：',arr.dtype)\n",
    "print('元素的转置：',arr.T)"
   ],
   "id": "1e02276c425a6e71",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "数组的形状： ()\n",
      "数组的维度： 0\n",
      "元素的个数： 1\n",
      "元素的数据类型： int64\n",
      "元素的转置： 1\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T07:42:12.827713Z",
     "start_time": "2025-10-09T07:42:12.822325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2,3])\n",
    "print(arr)\n",
    "print('数组的形状：',arr.shape)\n",
    "print('数组的维度：',arr.ndim)\n",
    "print('元素的个数：',arr.size)\n",
    "print('元素的数据类型：',arr.dtype)\n",
    "print('元素的转置：',arr.T)"
   ],
   "id": "1f2dc7439e1c018a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "数组的形状： (3,)\n",
      "数组的维度： 1\n",
      "元素的个数： 3\n",
      "元素的数据类型： int64\n",
      "元素的转置： [1 2 3]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr)\n",
    "print('数组的形状：',arr.shape)\n",
    "print('数组的维度：',arr.ndim)\n",
    "print('元素的个数：',arr.size)\n",
    "print('元素的数据类型：',arr.dtype)\n",
    "print('元素的转置：',arr.T)"
   ],
   "id": "96f9f99d52954069",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.ndarray的创建",
   "id": "2c501ff8b5442c9b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:01:22.005439Z",
     "start_time": "2025-10-09T09:01:22.000454Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 基础的创建方法\n",
    "list1 = [4,5,6]\n",
    "arr = np.array(list1,dtype=np.float64)\n",
    "print(arr)"
   ],
   "id": "e0412a859f5eb14f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4. 5. 6.]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:03:36.235415Z",
     "start_time": "2025-10-09T09:03:36.229432Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# copy\n",
    "arr1 = np.copy(arr) # 元素跟原始的数组相同，但是不是一个数组了\n",
    "print(arr1)\n",
    "arr1[0] = 8\n",
    "print(arr1)\n",
    "print(arr)"
   ],
   "id": "5ddf3b8df14bd0c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4. 5. 6.]\n",
      "[8. 5. 6.]\n",
      "[4. 5. 6.]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:15:38.371548Z",
     "start_time": "2025-10-09T09:15:38.367559Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 预定义形状\n",
    "# 全0 全1 未初始化 固定值\n",
    "# 全0\n",
    "arr = np.zeros((2,3),dtype=int)\n",
    "print(arr)\n",
    "print(arr.dtype)"
   ],
   "id": "391f41ae62220b62",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]]\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:19:41.273364Z",
     "start_time": "2025-10-09T09:19:41.268857Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#\n",
    "arr = np.zeros((200,),dtype=int)\n",
    "print(arr)"
   ],
   "id": "f197dae2d1b84329",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:23:27.236862Z",
     "start_time": "2025-10-09T09:23:27.231371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 全1\n",
    "arr = np.ones((5,8),dtype=int)\n",
    "print(arr)"
   ],
   "id": "137b48ff5bcefad5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:24:25.468100Z",
     "start_time": "2025-10-09T09:24:25.463413Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 未初始化\n",
    "arr = np.empty((2,3))\n",
    "print(arr)"
   ],
   "id": "2c262f3f25f269cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5.e-324 5.e-324 5.e-324]\n",
      " [5.e-324 5.e-324 5.e-324]]\n"
     ]
    }
   ],
   "execution_count": 96
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:40:42.191872Z",
     "start_time": "2025-10-09T09:40:42.187373Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.full((3,4),2025)\n",
    "print(arr)"
   ],
   "id": "80b64b51dd11ce85",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2025 2025 2025 2025]\n",
      " [2025 2025 2025 2025]\n",
      " [2025 2025 2025 2025]]\n"
     ]
    }
   ],
   "execution_count": 99
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-09T09:43:00.213106Z",
     "start_time": "2025-10-09T09:43:00.207657Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.zeros_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.empty_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.ones_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.full_like(arr,2026)\n",
    "print(arr1)"
   ],
   "id": "73c04f668015afd5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n",
      "[[2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]]\n",
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]]\n",
      "[[2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]]\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:40.004394Z",
     "start_time": "2025-10-12T05:35:39.995754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等差数列 1 2 3 4 5\n",
    "arr = np.arange(2,10,2) # start,end,step(步长)\n",
    "print(arr)"
   ],
   "id": "19dd7e0b9ac78dbc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6 8]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:41.483992Z",
     "start_time": "2025-10-12T05:35:41.477306Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等间隔数列\n",
    "arr = np.linspace(0,10,3)\n",
    "print(arr)"
   ],
   "id": "9938ce61ebb847c4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  5. 10.]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:51.277723Z",
     "start_time": "2025-10-12T05:35:51.268493Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.linspace(0,100,5,dtype=int)\n",
    "print(arr)\n",
    "arr = np.arange(0,101,25,dtype=int)\n",
    "print(arr)"
   ],
   "id": "666f8c37d33f8b11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0  25  50  75 100]\n",
      "[  0  25  50  75 100]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:55.373382Z",
     "start_time": "2025-10-12T05:35:55.366286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对数间隔数列\n",
    "arr = np.logspace(0,4,2,base=2)\n",
    "print(arr)"
   ],
   "id": "802306480eabd296",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1. 16.]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:59.014203Z",
     "start_time": "2025-10-12T05:35:59.004709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.linspace(0,4,3)\n",
    "print(arr)"
   ],
   "id": "d348fbba80a7b018",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 2. 4.]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:36:05.191805Z",
     "start_time": "2025-10-12T05:36:05.185146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.logspace(0,4,3,base=3)\n",
    "print(arr)"
   ],
   "id": "ef3e667d758586d9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  9. 81.]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:31.358636Z",
     "start_time": "2025-10-12T05:35:31.351100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 特殊矩阵\n",
    "# 单位矩阵：主对角线上的数字为1，其他数字为0\n",
    "arr = np.eye(3,4,dtype=int)\n",
    "print(arr)"
   ],
   "id": "154fa1b1ee9b23f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:35:25.627078Z",
     "start_time": "2025-10-12T05:35:25.612396Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对角矩阵：主对角线上非0的数字，其他的数字为0\n",
    "arr = np.diag([1,2,3])\n",
    "print(arr)"
   ],
   "id": "503a7acaca0d0eba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0]\n",
      " [0 2 0]\n",
      " [0 0 3]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:40:39.230445Z",
     "start_time": "2025-10-12T05:40:39.218823Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机数组的生成\n",
    "# 生成0到1之间的随机浮点数（均匀分布）\n",
    "arr = np.random.rand(2,3)\n",
    "print(arr)"
   ],
   "id": "4d840fa0e37ba32a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.09047813 0.0764277  0.42869861]\n",
      " [0.24752351 0.08296212 0.37006336]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:42:49.283376Z",
     "start_time": "2025-10-12T05:42:49.280030Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机浮点数\n",
    "arr = np.random.uniform(3,6,(2,3))\n",
    "print(arr)"
   ],
   "id": "d47f937e002b800f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.27812937 3.60449997 5.02629934]\n",
      " [4.40880975 3.83062623 3.06773275]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:44:23.609224Z",
     "start_time": "2025-10-12T05:44:23.594371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机整数\n",
    "arr = np.random.randint(3,30,(2,3))\n",
    "print(arr)"
   ],
   "id": "f0db1427b9620e7b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14  8  8]\n",
      " [24 21 21]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:49:09.138544Z",
     "start_time": "2025-10-12T05:49:09.128836Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成随机数列（正态分布）（-3到3之间）\n",
    "# 两边小，中间大\n",
    "arr = np.random.randn(2,3)\n",
    "print(arr)"
   ],
   "id": "d8828e8a76b7cefb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.03542232 -0.11117521 -0.10604538]\n",
      " [ 0.55589034 -0.06500577 -0.43661191]]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T05:51:09.658978Z",
     "start_time": "2025-10-12T05:51:09.647079Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置随机种子\n",
    "np.random.seed(20)\n",
    "arr = np.random.randint(1,10,(2,5))\n",
    "print(arr)"
   ],
   "id": "a190c1fec089d26",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5 7 8 3]\n",
      " [1 7 9 6 4]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 4、ndarry的数据类型\n",
    "布尔类型 bool\n",
    "\n",
    "整数类型 int uint\n",
    "\n",
    "浮点数 float\n",
    "\n",
    "复数 complex"
   ],
   "id": "5832acfa5eafcaca"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:09:52.461928Z",
     "start_time": "2025-10-12T06:09:52.447097Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,0,127,0],dtype=np.int8)\n",
    "print(arr)"
   ],
   "id": "296c5da7c2553b5f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1   0 -24   0]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5.索引与切片",
   "id": "e3b4fea2e1cf419c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:13:53.217033Z",
     "start_time": "2025-10-12T06:13:53.205045Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组的索引与切片\n",
    "arr = np.random.randint(1,100,20)"
   ],
   "id": "91591b12351ed86a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[41 12 70  1 59 87 38 69 19 10 58 15 36 76 24 30 61 98  3 20]\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:14:31.270781Z",
     "start_time": "2025-10-12T06:14:31.255030Z"
    }
   },
   "cell_type": "code",
   "source": "print(arr)",
   "id": "15c11221c59006f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[41 12 70  1 59 87 38 69 19 10 58 15 36 76 24 30 61 98  3 20]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:20:14.949468Z",
     "start_time": "2025-10-12T06:20:14.938149Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr[10])\n",
    "print(arr[:]) # 获取全部的数据\n",
    "print(arr[2:5]) # start:end + 1 左闭右开\n",
    "print(arr[slice(2,15,3)]) # start,end,step\n",
    "print(arr[ (arr>10) & (arr<70)]) # 布尔索引"
   ],
   "id": "98ff125d24468c3b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "58\n",
      "[41 12 70  1 59 87 38 69 19 10 58 15 36 76 24 30 61 98  3 20]\n",
      "[70  1 59]\n",
      "[70 87 19 15 24]\n",
      "[41 12 59 38 69 19 58 15 36 24 30 61 20]\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:23:23.935105Z",
     "start_time": "2025-10-12T06:23:23.917319Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维数组的索引与切片\n",
    "arr = np.random.randint(1,100,(4,8))\n",
    "print(arr)"
   ],
   "id": "acdd07732fb7be83",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[87 46 86 85 85 61 46 25]\n",
      " [76 56 66 83 52 13 37 42]\n",
      " [75 97 96 86 32 68 77 43]\n",
      " [ 3 40 97 87 80  8 12 86]]\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:33:49.652456Z",
     "start_time": "2025-10-12T06:33:49.637584Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr[1,3]) #索引\n",
    "print(arr[1,2:5]) # 切片\n",
    "print(arr[2][arr[2]>50])\n",
    "print(arr[:,3])"
   ],
   "id": "50812035d8eb8ed6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "83\n",
      "[66 83 52]\n",
      "[75 97 96 86 68 77]\n",
      "[85 83 86 87]\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5、 ndarry的运算",
   "id": "55243af84e3a3383"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:40:24.871542Z",
     "start_time": "2025-10-12T06:40:24.857675Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 算数运算\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "print(a+b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a ** 2)"
   ],
   "id": "8b7179c0b8c1fda",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[-3 -3 -3]\n",
      "[ 4 10 18]\n",
      "[0.25 0.4  0.5 ]\n",
      "[1 4 9]\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:38:24.142845Z",
     "start_time": "2025-10-12T06:38:24.099280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "c = [1,2,3]\n",
    "d = [4,5,6]\n",
    "print(c+d)\n",
    "print(c-d)"
   ],
   "id": "7702dfe0e89fdfb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'list' and 'list'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp\\ipykernel_26460\\1188626761.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[0md\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;33m[\u001B[0m\u001B[1;36m4\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m5\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m6\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mc\u001B[0m\u001B[1;33m+\u001B[0m\u001B[0md\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 4\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mc\u001B[0m\u001B[1;33m-\u001B[0m\u001B[0md\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m: unsupported operand type(s) for -: 'list' and 'list'"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:41:56.730371Z",
     "start_time": "2025-10-12T06:41:56.722702Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "b = np.array([[4,5,6],[7,8,9],[1,2,3]])\n",
    "print(a+b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a ** 2)"
   ],
   "id": "f9e3e8daab82a781",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  7  9]\n",
      " [11 13 15]\n",
      " [ 8 10 12]]\n",
      "[[-3 -3 -3]\n",
      " [-3 -3 -3]\n",
      " [ 6  6  6]]\n",
      "[[ 4 10 18]\n",
      " [28 40 54]\n",
      " [ 7 16 27]]\n",
      "[[0.25       0.4        0.5       ]\n",
      " [0.57142857 0.625      0.66666667]\n",
      " [7.         4.         3.        ]]\n",
      "[[ 1  4  9]\n",
      " [16 25 36]\n",
      " [49 64 81]]\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:43:55.572381Z",
     "start_time": "2025-10-12T06:43:55.544633Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组与标量之间的算术运算\n",
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(a+3)\n",
    "print(a * 3)"
   ],
   "id": "a31e4d2b89c65ed2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 3  6  9]\n",
      " [12 15 18]\n",
      " [21 24 27]]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T06:47:31.170896Z",
     "start_time": "2025-10-12T06:47:31.162483Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制: 1、获取形状 2、是否可广播\n",
    "# 同一维度：行与行（相同或有一个为1），列与列（相同或有一个为1）\n",
    "a = np.array([1,2,3]) # 1*3\n",
    "b = np.array([[4],[5],[6]]) #3*1\n",
    "print(a + b)\n",
    "'''\n",
    "a\n",
    "1 2 3\n",
    "1 2 3\n",
    "1 2 3\n",
    "b\n",
    "4 4 4\n",
    "5 5 5\n",
    "6 6 6\n",
    "'''"
   ],
   "id": "b4cbdcf618bae1c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6 7]\n",
      " [6 7 8]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制: 1、获取形状 2、是否可广播\n",
    "# 同一维度：相同、1\n",
    "a = np.array([1,2,3]) # 1*3\n",
    "b = np.array([[4,5]]) #1*2\n",
    "print(a + b)"
   ],
   "id": "535107f49165726d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "# 矩阵运算\n",
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "b = np.array([[4,5,6],[7,8,9],[1,2,3]])\n",
    "print(a * b)\n",
    "print(a @ b)"
   ],
   "id": "57aa50b2b6793621",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "a\n",
    "1 2 3\n",
    "4 5 6\n",
    "7 8 9\n",
    "b\n",
    "4 5 6\n",
    "7 8 9\n",
    "1 2 3\n",
    "\n",
    "a 1行 1 2 3\n",
    "b 1列 4 7 1\n",
    "1*4+2*7+3*1=21\n",
    "\n",
    "\n",
    "'''"
   ],
   "id": "220cf2174077965"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 2.3 numpy中的常用函数\n",
    "## 1.基本数学函数"
   ],
   "id": "1bb551973a29a5b3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T11:23:40.296198Z",
     "start_time": "2025-10-12T11:23:40.284996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平方根\n",
    "print(np.sqrt(9))\n",
    "print(np.sqrt([1,4,9]))\n",
    "arr = np.array([1,25,81])\n",
    "print(np.sqrt(arr))"
   ],
   "id": "53b671450f50cf4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "[1. 2. 3.]\n",
      "[1. 5. 9.]\n"
     ]
    }
   ],
   "execution_count": 92
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T11:27:01.799693Z",
     "start_time": "2025-10-12T11:27:01.791232Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算指数 e^x\n",
    "print(np.exp(1))"
   ],
   "id": "7d1d064303e34158",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.718281828459045\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T12:00:59.478039Z",
     "start_time": "2025-10-12T12:00:59.459496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算自然对数 lnx\n",
    "print(np.log(2.71))"
   ],
   "id": "a5ed5f5df8d69271",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9969486348916096\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T12:42:17.291202Z",
     "start_time": "2025-10-12T12:42:17.281358Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算正弦值、余弦值\n",
    "print(np.sin(np.pi/2))\n",
    "print(np.cos(np.pi))"
   ],
   "id": "ef112273a11e9afe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "-1.0\n"
     ]
    }
   ],
   "execution_count": 98
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T12:43:36.861045Z",
     "start_time": "2025-10-12T12:43:36.845671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算绝对值\n",
    "arr = np.array([-1,1,2,-3])\n",
    "print(np.abs(arr))"
   ],
   "id": "7e39edeb2d421822",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 2 3]\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T12:49:49.751657Z",
     "start_time": "2025-10-12T12:49:49.735076Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算a的b次幂\n",
    "print(np.power(arr,3))"
   ],
   "id": "c600bd7b3e587d11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ -1   1   8 -27]\n"
     ]
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T12:50:36.937561Z",
     "start_time": "2025-10-12T12:50:36.930983Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 四舍五入\n",
    "print(np.round([3.2,4.5,8.1,9.6]))"
   ],
   "id": "7add918229b0f162",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.  4.  8. 10.]\n"
     ]
    }
   ],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-12T13:23:36.284463Z",
     "start_time": "2025-10-12T13:23:36.264669Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 向上取整，向下取整\n",
    "arr = np.array([1.6,25.1,81.7])\n",
    "print(np.ceil(arr))\n",
    "print(np.floor(arr))"
   ],
   "id": "7baf037a8afa993f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2. 26. 82.]\n",
      "[ 1. 25. 81.]\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T01:44:19.843915Z",
     "start_time": "2025-10-13T01:44:19.832944Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测缺失值 NaN\n",
    "np.isnan([1,2,np.nan,3])"
   ],
   "id": "d348c1ce9abf5446",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 2、统计函数\n",
    "\n",
    "求和、计算平均值、计算中位数、标准差、方差\n",
    "\n",
    "查找最大值、最小值\n",
    "\n",
    "计算分位数、累积和、累计差"
   ],
   "id": "d67aedcb64d3ad83"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T01:44:35.668324Z",
     "start_time": "2025-10-13T01:44:35.663337Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,20,8)\n",
    "print(arr)"
   ],
   "id": "406ba53c85ac45f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12  1  6  2  8  2  9  2]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 求和\n",
    "print(np.sum([1,2,3]))"
   ],
   "id": "3ad5f4fcf64e3334"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 计算平均值\n",
    "print(np.mean(arr))"
   ],
   "id": "7ab184420d12e887"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 计算中位数 奇数：排序后中间的数值  偶数：中间的两个数的平均值\n",
    "print(np.median([1,2,3]))\n",
    "print(np.median([1,2,3,4]))"
   ],
   "id": "cfcda2b406944bbc"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#计算标准差、方差\n",
    "# 1,2,3 的平均值 2\n",
    "# (1-2)^2 + (2-2)^2 + (3-2)^2 / 3 = 0.66 # 方差\n",
    "print(np.var([1,2,3])) # 方差\n",
    "print(np.std([1,2,3])) # 标准差"
   ],
   "id": "5a9ffc28beb9308e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 计算最大值、最小值\n",
    "print(np.max(arr),np.argmax(arr))\n",
    "print(np.min(arr),np.argmin(arr))"
   ],
   "id": "21eca0fac0e2617e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 分位数\n",
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,100,4)\n",
    "print(arr)\n",
    "print(np.median(arr))\n",
    "print(np.percentile(arr,20))"
   ],
   "id": "81ae25a5d6ba5ad0"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 累积和、累积积\n",
    "arr = np.array([1,2,3])\n",
    "print(np.sum(arr))\n",
    "print(np.cumsum(arr))\n",
    "print(np.cumprod(arr))"
   ],
   "id": "cdcbd87032ffc2b2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3、比较函数\n",
    "\n",
    "比较是否大于、小于、等于\n",
    "\n",
    "逻辑与、或、非\n",
    "\n",
    "检查数组中是否有一个True，是否所有的都为True"
   ],
   "id": "79ee0b63ccf24f6c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 是否大于\n",
    "print(np.greater([3,4,5,6,7],4))\n",
    "# 是否小于\n",
    "print(np.less([3,4,5,6,7],4))\n",
    "# 是否等于\n",
    "print(np.equal([3,4,5,6,7,8],4))\n",
    "print(np.equal([3,4,5],[4,4,4]))"
   ],
   "id": "a8adadf8492b1d51"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "print(np.logical_and([0,0],[1,1]))\n",
    "print(np.logical_or([0,0],[1,1]))\n",
    "print(np.logical_not([1,0]))"
   ],
   "id": "16b8bf437796c84d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 检查元素 是否至少有一个元素为True\n",
    "print(np.any([0,0,0,0,1]))\n",
    "# 检查元素 是否全部元素都为True\n",
    "print(np.all([1,1,1,1,1]))"
   ],
   "id": "802d8e8d0268e80c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 自定义条件\n",
    "# print(np.where(条件，符合条件，不符合条件的))\n",
    "arr = np.array([1,2,3,4,5,6])\n",
    "print(np.where(arr<3,arr,0))"
   ],
   "id": "e941347283164f2b"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "print(np.where(arr<3,1,0))"
   ],
   "id": "4647f3c3958ce516"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "score = np.random.randint(50,100,20)\n",
    "print(score)\n",
    "print(np.where(score>=60,'及格','不及格'))"
   ],
   "id": "c37c4a2a37d5067"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "print(np.where(\n",
    "    score<60,'不及格',np.where(\n",
    "        score<80,'良好','优秀'\n",
    "    )\n",
    "))"
   ],
   "id": "f7785576b50c78ff"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# np.select(条件，返回的结果)\n",
    "print(np.select([score>80,(score>=60) & (score<=80),score<60],\n",
    "                ['优秀','良好','不及格'],\n",
    "                default='未知'))\n"
   ],
   "id": "1cd0a88b6140d4fa"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4、排序函数",
   "id": "d8683181641fc6a7"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "np.random.seed(0)\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)\n",
    "# print(arr.sort()) # 改变原数组\n",
    "# print(arr)\n",
    "print(np.sort(arr))\n",
    "print(np.argsort(arr))\n",
    "print(arr)"
   ],
   "id": "d70d52919fd1a595"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 去重函数\n",
    "print(np.unique(arr))"
   ],
   "id": "d1deb6c30de407df"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 数组的拼接\n",
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([4,5,6])\n",
    "print(np.concatenate((arr1,arr2)))"
   ],
   "id": "1455cb69f2f0e9e2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 数组的分割\n",
    "print(np.split(arr,[6,12,18]))"
   ],
   "id": "506c1d31f288b604"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 调整数组的形状\n",
    "print(np.reshape(arr,[2,10]))"
   ],
   "id": "68fabdda7cfa99e5"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 第2章 NumPy科学计算综合练习",
   "id": "adad4a2e82b348d1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目 1：温度数据分析\n",
    "某城市一周的最高气温（℃）为 [28,30,29,31,32,30,29]。\n",
    "- 计算平均气温、最高气温和最低气温\n",
    "- 找出气温超过 30°C 的天数\n",
    "'''"
   ],
   "id": "bd6baa89c8270543"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import numpy as np\n",
    "temps = np.array([28,30,29,31,32,30,29])\n",
    "print('平均气温：%.3f' % np.mean(temps))\n",
    "print('最高气温：%d' % np.max(temps))\n",
    "print('最低气温：%d' % np.min(temps))\n",
    "print('气温超过30°c的天数：')\n",
    "print(len(temps[temps>30]))\n",
    "print(np.cumsum(np.where(temps>30,1,0))[-1])\n",
    "print(np.count_nonzero(temps>30))"
   ],
   "id": "4d0077754f52cc01"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目2：学生成绩统计\n",
    "某班级 5 名学生的数学成绩为 [85，90，78，92，88]。\n",
    "- 计算成绩的平均分、中位数和标准差\n",
    "- 将成绩转换成十分制（假设满分为10）。\n",
    "'''"
   ],
   "id": "5f24f1df3aaca92a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "score = np.array([85,90,78,92,88])\n",
    "print(score)\n",
    "print('平均分：',np.mean(score))\n",
    "print('中位数：',np.median(score))\n",
    "print('标准差：%.3f' % np.std(score))\n",
    "print(score/10)"
   ],
   "id": "53edb7de00b7addd"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目4：随机数据生成\n",
    "生成一个 （3,4） 的随机整数数组，范围[0,10)\n",
    "- 计算每列的最大值和每行的最小值\n",
    "- 将数组中的所有奇数替换为 -1\n",
    "'''"
   ],
   "id": "ee11d2688a87ee9a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,10,(3,4))\n",
    "print(arr)\n",
    "print('每列的最大值',np.max(arr,axis=0)) # axis=0 列 axis=1 行\n",
    "print('每行的最小值',np.min(arr,axis=1))\n",
    "print(np.where(arr%2==1,-1,arr))\n",
    "arr[arr%2==1] = -1\n",
    "print(arr%2==1)\n",
    "print(arr)"
   ],
   "id": "7bc5731a703d2470"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目5：数组变形\n",
    "创建一个 1 到 12 的一维数组，并转换为 (3，4)的二维数组\n",
    "- 计算每行的和与每列的平均值。\n",
    "- 将数组展平为一维数组。\n",
    "'''"
   ],
   "id": "6be80749cd68fbab"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "arr = np.arange(1,13,1)\n",
    "print(arr)\n",
    "arr = np.reshape(arr,(3,4))\n",
    "print(arr)\n",
    "print('每行的和',np.sum(arr,axis=1))\n",
    "print('每列的平均值',np.mean(arr,axis=0))\n",
    "arr = np.reshape(arr,(12))\n",
    "print(arr)"
   ],
   "id": "43fc98605ad4ecbf"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "生成一个 (5,5)的随机数组，范围[0,20)。\n",
    "- 找出数组中大于 10 的元素。\n",
    "- 将所有大于 10 的元素替换为 0。\n",
    "'''"
   ],
   "id": "7dc717a7bf85bbc9"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,20,(5,5))\n",
    "print(arr)\n",
    "print(arr[arr>10])\n",
    "arr[arr>10] = 0\n",
    "print(arr)\n"
   ],
   "id": "b937a4a50e90bba8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目 7 ：统计函数应用\n",
    "某公司 6 个月的销售额(万元) 为 [120，135，110，125，130，140]。\n",
    "- 计算销售额的总和、均值和方差。\n",
    "- 找出销售额最高的月份和最低的月份。\n",
    "'''"
   ],
   "id": "14bbbefe4f28f987"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "arr = np.array([120,135,110,125,130,140])\n",
    "print('销售额总和：',np.sum(arr))\n",
    "print('销售额均值：',np.mean(arr))\n",
    "print('销售额方差：',np.var(arr))\n",
    "print('销售额最高的月份：',np.argmax(arr)+1)\n",
    "print('销售额最低的月份：',np.argmin(arr)+1)"
   ],
   "id": "7ba65d8a0f6db440"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目8：\n",
    "给定 A = [1,2,3]和B = [4,5,6]\n",
    "- 将 A 和 B 水平拼接为一个新数组\n",
    "- 将 A 和 B 垂直拼接为一个新数组\n",
    "'''"
   ],
   "id": "7fa939d374f876c3"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "c = np.concatenate([a,b])\n",
    "print('水平拼接：',c)\n",
    "print('垂直拼接：',np.reshape(c,(2,3)))"
   ],
   "id": "a04d593d7db6ed12"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目9：唯一值与排序\n",
    "给定数组[2,1,2,3,1,4,3]。\n",
    "- 找出数组中唯一值并排序。\n",
    "- 计算每个唯一值出现的次数。\n",
    "'''"
   ],
   "id": "283e60eb8a1d934e"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "arr = np.array([2,1,2,3,1,4,3])\n",
    "u_arr,counts = np.unique(arr,return_counts=True)\n",
    "print(u_arr,counts)"
   ],
   "id": "595ccd9552b30ad4"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "'''\n",
    "题目 20 ：综合应用\n",
    "某商店 5 天的销售额（万元）和成本（万元）如下：\n",
    "销售额：[20,25,22,30,28]\n",
    "成本：[15,18,16,22,20]\n",
    "- 计算每天的利润（销售额-成本）\n",
    "- 计算利润的平均值和标准差\n",
    "- 找出利润最高的天数\n",
    "'''"
   ],
   "id": "aa4b84a9845aae0a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-13T12:50:13.168176Z",
     "start_time": "2025-10-13T12:50:13.160653Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sell = np.array([20,25,22,30,28])\n",
    "cost = np.array([15,18,16,22,20])\n",
    "profit = sell - cost\n",
    "print('每天利润：',profit)\n",
    "print('利润平均值：',np.mean(profit))\n",
    "print('利润标准差：',np.std(profit))\n",
    "print('利润最高的天数：',len(profit[profit == np.max(profit)]))\n"
   ],
   "id": "e8356962c9b3254b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每天利润： [5 7 6 8 8]\n",
      "利润平均值： 6.8\n",
      "利润标准差： 1.16619037896906\n",
      "利润最高的天数： 2\n"
     ]
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
